package com.orangelife.mobile.confirmOrder.biz;

import android.app.Activity;
import android.os.Handler;

import com.common.jump.PayJumper;
import com.curry.android.http.HttpUtil;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.StringUtil;
import com.curry.android.util.ToastHelper;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.constants.ConfirmOrderURL;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 确认订单（生成支付流水号）服务端请求；
 * <p>
 * Created by wgf on 2016/2/24.
 *
 * @author wgf
 * @since v3.4.1
 */
public class ConfirmOrderHttpBiz {

    /** 周边商品下单 */
    public static void commitOrder(Map<String, Object> orderParamMap, Handler handler, int what) {
        JSONRequest.getInstance().sendRequest(JSONRequest.POST, ConfirmOrderURL.COMMIT_ORDER, orderParamMap, handler, what, HttpUtil.ACTION_JSON);
    }

    /** 特惠下单 */
    public static void commitOrder4Tehui(Map<String, Object> orderParamMap, Handler handler, int what) {
        JSONRequest.getInstance().sendRequest(JSONRequest.POST, ConfirmOrderURL.COMMIT_TEHUI_ORDER, orderParamMap, handler, what, HttpUtil.ACTION_JSON);
    }

    /** 应缴账单中下单，生成订单号和订单流水号 */
    public static void genOrder(String commID, String apartmentID, List<String> feeDetailIDs, ArrayList<String> voucherIDs,
                                String deductionMoney, String totalAmount, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("commID", commID);
        jsonMap.put("apartmentID", apartmentID);
        jsonMap.put("feeDetailIDs", feeDetailIDs);
        String oweMoney = "0";
        String subMoney = "0";
        if (deductionMoney != null) {
            if (deductionMoney.contains("-")) {
                oweMoney = deductionMoney;//欠款
            } else {
                subMoney = deductionMoney;//余额
            }
        }
        jsonMap.put("oweMoney", oweMoney);//欠款
        jsonMap.put("subMoney", subMoney);//余额
        jsonMap.put("totalCost", totalAmount);//金额
        if (voucherIDs != null && voucherIDs.size() > 0) {
            jsonMap.put("vouchers", voucherIDs);//抵用券
        }
        VolleyHttpUtil.getInstance().postWithCallback(ConfirmOrderURL.GEN_ORDER, jsonMap, callback);
    }

    /** 处理物业缴费的下单返回值 */
    public static void handleGenOrder(Activity activity, JSONObject response, String amountPaid) {
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            Map<String, Object> map = OrangeDataHandler.getMapWithEntityNoGson(response);
            if (map != null) {
                ExitApplication.getInstance().addPaidActivity(activity);
                if (!"0.00".equals(amountPaid)) {
                    PayJumper.jumpToPayFromProperty(activity, map);
                } else {
                    PayJumper.jumpToPaidResultWithFree(activity, map);
                }
            }
        }
    }

    /** 旧物业缴费的下单 */
    public static void genOrderOld(String commID, String orderID, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("commID", commID);
        jsonMap.put("orderID", orderID);
        VolleyHttpUtil.getInstance().postWithCallback(ConfirmOrderURL.GEN_ORDER, jsonMap, callback);
    }

    public static void handleOutTrade(Activity activity, JSONObject response) {
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            Map<String, Object> map = OrangeDataHandler.getMapWithEntityNoGson(response);
            if (map != null) {
                String outTradeNo = String.valueOf(map.get("outtradeno"));//订单流水号
                String orderID = String.valueOf(map.get("orderID"));
                if (!StringUtil.isBlank(outTradeNo) || !StringUtil.isBlank(orderID)) {
                    ExitApplication.getInstance().addPaidActivity(activity);
                    PayJumper.jumpToPayFromProperty(activity, map);
                } else {
                    ToastHelper.getInstance()._toast("请求失败..");
                }
            } else {
                ToastHelper.getInstance()._toast("请求失败.");
            }
        }
    }

}
